package com.ygqh.baby.controller.kids.model;

import com.ygqh.baby.ao.*;
import com.ygqh.baby.po.YgReturnOrder;
import com.ygqh.baby.po.YgReturnOrderDetail;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: sunshuo
 * @date: 2018/11/8 15:16
 */
public class YgReturnOrderBuilder {

	private KidsReturnOrder kidsReturnOrder;

	private List<KidsReturnOrderDetail> kidsReturnOrderDetailList;

	private List<Long> orderDetailIdByRefund = new ArrayList<>();

	public YgReturnOrderBuilder(KidsReturnOrder kidsReturnOrder, List<KidsReturnOrderDetail> kidsReturnOrderDetailList) {
		this.kidsReturnOrder = kidsReturnOrder;
		this.kidsReturnOrderDetailList = kidsReturnOrderDetailList;
	}

	public List<Long> getOrderDetailIdByRefund() {
		return orderDetailIdByRefund;
	}

	public YgReturnOrder builder() {
		Assert.notNull(kidsReturnOrder, "新世界换货单信息不能为空");
		Assert.notEmpty(kidsReturnOrderDetailList, "新世界换货单明细不能为空");
		YgReturnOrder returnOrder = builderReturnOrder();
		returnOrder.setReturnOrderDetails(builderReturnOrderDetail());
		return returnOrder;
	}

	private List<YgReturnOrderDetail> builderReturnOrderDetail() {
		List<YgReturnOrderDetail> returnOrderDetailList = new ArrayList<>(kidsReturnOrderDetailList.size());
		kidsReturnOrderDetailList.forEach(kidsReturnOrderDetail -> {
			YgReturnOrderDetail returnOrderDetail = new YgReturnOrderDetail();
			BeanUtils.copyProperties(kidsReturnOrderDetail, returnOrderDetail);

			returnOrderDetail.setPrice(BigDecimal.ZERO);
			returnOrderDetail.setShortName(kidsReturnOrderDetail.getColor());
			returnOrderDetail.setDetailStatus(OrderDetailStatus.WaitingSend);
			returnOrderDetail.setRemark(kidsReturnOrderDetail.getId().toString());
			returnOrderDetail.setStatus(DataStatus.Valid);
			returnOrderDetailList.add(returnOrderDetail);
		});
		return returnOrderDetailList;
	}

	private YgReturnOrder builderReturnOrder() {
		YgReturnOrder returnOrder = new YgReturnOrder();
		BeanUtils.copyProperties(kidsReturnOrder, returnOrder);

		returnOrder.setOrderId(Long.valueOf(kidsReturnOrder.getOrderCode()));
		returnOrder.setReturnType(ReturnType.Change);
		returnOrder.setReturnMode(ReturnMode.OnLine);
		returnOrder.setReturnStatus(ReturnStatus.WaitingSend);
		returnOrder.setUserExpressCompany(kidsReturnOrder.getUserLogisticCompany());
		returnOrder.setUserExpressCode(kidsReturnOrder.getUserLogisticCode());
		returnOrder.setSendCount(0L);
		if (returnOrder.getReturnImgPath() == null) {
			returnOrder.setReturnImgPath("");
		}
		returnOrder.setStatus(DataStatus.Valid);

		returnOrder.setProductPrice(BigDecimal.ZERO);
		returnOrder.setAccountPrice(BigDecimal.ZERO);
		returnOrder.setRefundPrice(BigDecimal.ZERO);
		returnOrder.setRefundAccountPrice(BigDecimal.ZERO);
		returnOrder.setReturnPrice(BigDecimal.ZERO);
		returnOrder.setReturnReasonType(ReturnReasonType.Other);

		return returnOrder;
	}

}
